package com.jing.flashlight.application;

import java.util.UUID;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

public class MyApplication extends Application {

	private String udid = "";
	private String imei = "";

	@Override
	public void onCreate() {
		super.onCreate();
	}

	public String getUDID() {
		if (TextUtils.isEmpty(udid)) {
			initUDID();
		}
		return udid;
	}

	public String getIMEI() {
		if (TextUtils.isEmpty(imei)) {
			initIMEI();
		}
		return imei;
	}

	private void initUDID() {
		SharedPreferences mPerferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		SharedPreferences.Editor mEditor = mPerferences.edit();
		udid = mPerferences.getString("udid", "");
		if (TextUtils.isEmpty(udid)) {
			udid = MD5.MD5Encode(getIMEI());
			mEditor.putString("udid", udid);
			mEditor.commit();
		}
	}

	private void initIMEI() {
		SharedPreferences mPerferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		SharedPreferences.Editor mEditor = mPerferences.edit();
		imei = mPerferences.getString("imei", "");
		if (TextUtils.isEmpty(imei)) {
			TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
			imei = tm.getDeviceId();
			if (TextUtils.isEmpty(imei)) {
				imei = UUID.randomUUID().toString();
			}
			mEditor.putString("imei", imei);
			mEditor.commit();
		}
	}
}
